class Solution
{
public:
    int minAbsoluteDifference(vector<int> &nums, int x)
    {
        int n = nums.size();
        int minDiff = INT_MAX;
        set<int> numSet;

        for (int i = x; i < n; ++i)
        {
            numSet.insert(nums[i - x]);
            int num = nums[i];
            auto it = numSet.lower_bound(num);
            if (it != numSet.end())
            {
                minDiff = min(minDiff, abs(*it - num));
            }

            if (it != numSet.begin())
            {
                --it;
                minDiff = min(minDiff, abs(*it - num));
            }
        }
        return minDiff;
    }
};